//
//  CentresView.m
//  demo2
//
//  Created by mysj on 16/6/29.
//  Copyright © 2016年 zeng. All rights reserved.
//

#import "CampView.h"

//----------------------颜色类---------------------------
// rgb颜色转换（16进制->10进制）8b8787
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

//带有RGBA的颜色设置
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

// 获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)

#define  notName  @"CentresViewCansouSeletct"

#define HW    30

@interface CampView ()

@property (nonatomic, strong) NSMutableArray * imgViews;

@property (nonatomic, strong) NSMutableArray *btnArray;

@property (nonatomic, strong) NSMutableArray *animationImages;

@property (nonatomic, assign, getter=isSeletct) BOOL seletct;

@end


@implementation CampView

+ (instancetype)camp {
    return [[self alloc] initWithFrame:CGRectMake(-50, 0, HW, HW)];
}

- (NSMutableArray *)imgViews {
    
    if (_imgViews == nil) {
        _imgViews = [NSMutableArray arrayWithCapacity:8];
    }
    return _imgViews;
}

- (NSMutableArray *)btnArray {
   
    if (_btnArray == nil) {
        _btnArray = [NSMutableArray arrayWithCapacity:4];
    }
    return _btnArray;
}

- (NSMutableArray *)animationImages {
    
    if (_animationImages == nil) {
        _animationImages = [NSMutableArray arrayWithCapacity:6];
        for (int i = 0; i < 6; i ++) {
            UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"camp_seletct_%d.png", i]];
            [_animationImages addObject:img];
        }
    }
    return _animationImages;
}

#pragma mark- 点击

- (void)didseletct:(UIGestureRecognizer *)recognizer {
    if (self.isSeletct) {
        return;
    }
    self.seletct = YES;
 
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.center = recognizer.view.center;
    imageView.bounds = CGRectMake(0, 0, 100, 100);
    [self addbtn:recognizer.view];
    [self.superview addSubview:imageView];
    
    imageView.animationImages = self.animationImages;
    imageView.animationDuration = .6;
    imageView.animationRepeatCount = 6;
    [imageView startAnimating];
    
    CGFloat delay = imageView.animationDuration + .2;
    
    [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}

- (void)clearCache {
    
    self.animationImages = nil;
    
}

- (void)addbtn:(UIView *)view {
    
    for (int i = 0; i < 4; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"seletct_btn_0%d",i]];
        [button setImage:img forState:UIControlStateNormal];
        button.frame = view.frame;
        button.tag = i;
        [button addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        [self.superview addSubview:button];
        [self.btnArray addObject:button];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
    
        for (int i = 0; i < 4; i ++) {
            UIButton *button = self.btnArray[i];
            button.frame = [self getBtnFrame:i View:view];
        }
    }];
    
    
}

- (void)buttonTouchUpInside:(UIButton *)btn {
    
    if (btn.tag == 0) {
        NSLog(@"发射emp");
    } else if (btn.tag == 1) {
        // 不出 key入侵
    } else if (btn.tag == 2) {
        //入侵
        [[NSNotificationCenter defaultCenter] postNotificationName:notName object:nil];
    } else {
        // 聚能状态
    }
}

- (CGRect)getBtnFrame:(int)index View:(UIView *)view {

    if (index == 0) {
        return CGRectMake(view.frame.origin.x - 11, view.frame.origin.y - 30, 72, 17);
    } else if (index == 1) {
        return CGRectMake(view.frame.origin.x - 11, view.frame.origin.y + 66, 72, 17);
    } else if (index ==2) {
        return CGRectMake(view.frame.origin.x -92, view.frame.origin.y + 17.5, 72, 17);
    }
    return CGRectMake(view.frame.origin.x + 72, view.frame.origin.y + 17.5, 72, 17);
}


/*
- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context,1,0,1,1.0);
    CGContextSetLineWidth(context, 1.0);
    CGContextAddArc(context, rect.size.width * 0.5, rect.size.height * 0.5, 10, 0, 2 * M_PI, 0); //添加一个圆
    CGContextDrawPath(context, kCGPathStroke); //绘制路径
    CGContextDrawPath(context, kCGPathFill);//绘制填充
}
*/
#pragma mark- 初始化
- (instancetype)initWithFrame:(CGRect)frame {
   
    if (self = [super initWithFrame:frame]) {
        
        [self prepareItems];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cansouSeletct) name:notName object:nil];
    }
    return self;
}

- (instancetype)init {

    if (self = [super init]) {
        [self prepareItems];
    }
    return self;
}

- (void)prepareItems {
    NSString *imgName;
    imgName = self.isrbCamp ? @"centres_green.png" : @"centres_blue.png";
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
    [self addSubview:imageView];
    imageView.frame = CGRectMake(HW * 0.5 - 7, HW * 0.5 - 7, 14, 14);
    
    for (int i = 0; i < 8; i++) {
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"camp_item_0%d",i]];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
        [self addSubview:imageView];
        [self.imgViews addObject:imageView];
    
        imageView.hidden = NO;
        CGFloat rotate = [self rotateAngle:i];
        CGPoint loc = [self getImgCenterAngle:rotate];
        imageView.center = loc;
        double rotateAngle = [self redianIndex:i];
        CGAffineTransform transform = CGAffineTransformMakeRotation(rotateAngle);
        imageView.transform = transform;
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didseletct:)];
    [self addGestureRecognizer:tap];
}

- (void)cansouSeletct {
    self.seletct = NO;
    for (UIButton *button in self.btnArray) {
        [UIView animateWithDuration:0.4 animations:^{
            button.alpha = 0;
        }];
    }
    self.btnArray = nil;
}

- (CGFloat)rotateAngle:(int)index {
    switch (index) {
        case 0:
            return 90.0;
            break;
        case 1:
            return 45.0;
            break;
        case 3:
            return -45.0;
            break;
        case 4:
            return -90.0;
            break;
        case 5:
            return -135.0;
            break;
        case 6:
            return -180.0;
            break;
        case 7:
            return -225;
            break;
    }
    return 0;
}

- (double)redianIndex:(int)index {

    switch (index) {
        case 0:
            return -M_PI_2 * 2;
            break;
        case 1:
            return -(M_PI_2 + M_PI_4);
            break;
        case 2:
            return -M_PI_2;
            break;
        case 3:
            return -M_PI_4;
            break;
        case 5:
            return M_PI_4;
            break;
        case 6:
            return M_PI_2;
            break;
        case 7:
            return (M_PI_2 + M_PI_4);
            break;
    }
    return 0;
}

- (CGPoint)getImgCenterAngle:(CGFloat)angle {

    return [self calcCircleCoordinateWithCenter:CGPointMake(HW * .5, HW * .5) andWithAngle:angle andWithRadius:12];
}

#pragma mark 计算圆圈上点的坐标
- (CGPoint)calcCircleCoordinateWithCenter:(CGPoint)center
                             andWithAngle:(CGFloat)angle
                            andWithRadius: (CGFloat)radius {
    
    CGFloat x2 = radius * cosf(angle * M_PI / 180);
    CGFloat y2 = radius * sinf(angle * M_PI / 180);
    return CGPointMake(center.x + x2, center.y - y2);
}

@end
